package com.online_vote_system.demo.controller;

import com.online_vote_system.demo.service.AdminService;
import com.online_vote_system.demo.vo.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.List;

@Slf4j
@Controller
public class RootAdminController {

    @Autowired
    AdminService adminService;

    @GetMapping(value = {"/RootAdmin"})
    public String Audience(){
        return "html/AdminLogin";
    }

    @GetMapping("/AdminOs")
    public String AdminOs(Model model){
        List<Admin> list = adminService.getAllAdmin();
        System.out.println(list);
        model.addAttribute("Admin",list);
        return "html/RootAdmin/index";
    }

    @GetMapping("delAdmin/{adminId}")
    public String delAdmin(@PathVariable("adminId") String adminId,
                           Model model){
        Boolean result = adminService.removeById(adminId);
        if (result){
            model.addAttribute("msg","删除成功");
            return "redirect:/AdminOs";
        }else {
            model.addAttribute("msg","删除失败");
            return "redirect:/AdminOs";
        }
    }

    @GetMapping("updAdmin/{adminId}")
    public String updAdmin(@PathVariable("adminId") String adminId,
                           Model model){
        Admin admin = adminService.getById(adminId);
        model.addAttribute("admin",admin);
        return "html/RootAdmin/updAdmin";
    }

    @PostMapping("/updateAdmin")
    public String updateAdmin(String adminId,
                              String adminName,
                              String adminPass,
                              String adminType){
        Admin admin = new Admin();
        admin.setAdminId(Integer.valueOf(adminId));
        admin.setAdminName(adminName);admin.setAdminPass(adminPass);
        admin.setAdminType(adminType);
        adminService.updateById(admin);
        return "redirect:/AdminOs";
    }
    @GetMapping("/insAdmin")
    public String insAdmin(){
        return "html/RootAdmin/insAdmin";
    }
    @PostMapping("/insertAdmin")
    public String insertAdmin(String adminName,
                              String adminPass,
                              String adminType){
        Admin admin = new Admin();
        admin.setAdminType(adminType);admin.setAdminName(adminName);admin.setAdminPass(adminPass);
        adminService.save(admin);
        return "redirect:/AdminOs";
    }
}
